<template>
    <h1>VUE3.0学习---watch</h1>
    <div class = "test1">
        <h2>监视reactive定义的对象~默认会深度监视</h2>
        <p>姓名：{{ person.name }}</p>
        <p>地址：{{ person.addr.bAddr.cAddr }}</p>
        <button @click="changName">修改姓名</button>
        <button @click="changAddr">修改地址</button>
        <button @click="changPerson">修改整个人</button>
    </div>
</template>

<script lang="ts" setup>
   import { watch ,reactive} from 'vue';
   let person = reactive({
        name:"张三",
        addr:{bAddr:{cAddr:"钱塘镇"}}
    })
   function changName(){
    person.name += '~'
   }
   function changAddr(){
    person.addr.bAddr.cAddr = '太和镇'
   }
   function changPerson(){
    person = Object.assign(person,{name:"李四",addr:{bAddr:{cAddr:"云门镇"}}})
   }
   //默认会开启深度监视，就是对象的属性值变了也会被监视
   watch(person,(oldValue,newValue) => {
    console.log(oldValue,newValue)
   })
</script>

<style scoped>
 .test1{
    font-size: 20px;
    color: blue;
    background-color: greenyellow;
    border: 2px solid #007bff; /* 设置边框的宽度和颜色 */
    border-radius: 20px; /* 将四个角变成圆形 */
    width: auto; /* 设置元素的宽度 */
    height: auto; /* 设置元素的高度 */
    padding: 20px;
 }

 button{
    margin: 2px;
 }

</style>